/**
 * 
 */
package cc.rengu.redp.bizimpl.utils;

import cc.rengu.jrbdp.manager.ManageTool;
import cc.rengu.jrbdp.manager.client.ManagerClient;
import cc.rengu.jrbdp.register.realtime.client.RealTimeRegisterClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xu
 */
public class RbdpClient {

	private static final Logger logger = LoggerFactory.getLogger(RbdpClient.class);
	
	private static RealTimeRegisterClient client;
	private static ManagerClient managerClient;
	private static ManageTool manageTool;

	public static RealTimeRegisterClient getIntance1() {
		try {				
			if (client == null) {
				client = new RealTimeRegisterClient();
				register();
			}
			boolean isconnect = client.connect();
			if(!isconnect){
				String error = client.getError();
				logger.error("实时注册服务器连接异常:[" + error + "]");
			}
		} catch (Exception e) {
			logger.error("实时注册服务器连接异常:[" + e.getMessage() + "]");
		}
		return client;
	}

	public static ManagerClient getIntance2() {
		try{
			if (managerClient == null) {
				managerClient = new ManagerClient();
				managerClient();
			}
			boolean isconnect = managerClient.connect();
			if(!isconnect){
				String error = managerClient.getError();
				logger.error("管理服务器连接异常:[" + error + "]");
			}
		} catch (Exception e) {
			logger.error("管理服务器连接异常:[" + e.getMessage() + "]");
		}
		return managerClient;
	}

	public static ManageTool getIntance3() {
		try{
			if (manageTool == null) {
				manageTool();
			}
		} catch (Exception e) {
			logger.error("管理服务器工具初始化异常:[" + e.getMessage() + "]");
		}
		return manageTool;
	}
	
	private static void managerClient() {
//		Properties prop = PropertiesUtils.getProperties("/conf.properties");
//		String rbdpserver = prop.getProperty("rbdpserver_manager");

//		String rbdpserver = "59.110.215.135:12163:50000:50000";
		String rbdpserver = "172.17.100.9:62133:50000:50000";
		int id = 0;
		String[] serverList = rbdpserver.split(",");
		for (String server : serverList) {
			String[] argArr = server.split(":");
			String ipAddr = null;
			int port = 0;
			int connectTimeout = 10000;
			int readTimeout = 10000;
			if (argArr.length >= 2) {
				ipAddr = argArr[0];
				port = Integer.parseInt(argArr[1]);
			} else
				continue;
			if (argArr.length >= 3)
				connectTimeout = Integer.parseInt(argArr[2]);
			if (argArr.length >= 4)
				readTimeout = Integer.parseInt(argArr[3]);
			logger.info("服务器-{}: [{}:{}] 连接超时[{}], 读取超时[{}]\n", id,
					ipAddr, port, connectTimeout, readTimeout);
			managerClient.addServer(ipAddr, port, connectTimeout, readTimeout);
			id++;
		}
	}

	private static void register() {
//		Properties prop = PropertiesUtils.getProperties("/conf.properties");
//		String rbdpserver = prop.getProperty("rbdpserver_register");

//		String rbdpserver = "59.110.215.135:12164:50000:50000";
		String rbdpserver = "172.17.100.9:62133:50000:50000";
		int id = 0;
		String[] serverList = rbdpserver.split(",");
		for (String server : serverList) {
			String[] argArr = server.split(":");
			String ipAddr = null;
			int port = 0;
			int connectTimeout = 10000;
			int readTimeout = 10000;
			if (argArr.length >= 2) {
				ipAddr = argArr[0];
				port = Integer.parseInt(argArr[1]);
			} else
				continue;
			if (argArr.length >= 3)
				connectTimeout = Integer.parseInt(argArr[2]);
			if (argArr.length >= 4)
				readTimeout = Integer.parseInt(argArr[3]);

			logger.info("服务器-{}: [{}:{}] 连接超时[{}], 读取超时[{}]\n", id,
					ipAddr, port, connectTimeout, readTimeout);
			client.addServer(ipAddr, port, connectTimeout, readTimeout);
			id++;
		}
	}
	
	private static void manageTool() {
//		Properties prop = PropertiesUtils.getProperties("/conf.properties");
//		String rbdpserver = prop.getProperty("rbdpserver_manager");
		String rbdpserver = "172.17.100.9:62133:50000:50000";
//		String rbdpserver = "59.110.215.135:12163:50000:50000";
		String[] serverList = rbdpserver.split(",");
		manageTool=new ManageTool(serverList);
	}
	
}
